package com.zui.util;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class DateConverter extends StrutsTypeConverter {
	
	private static final Logger LOG = LoggerFactory.getLogger(DateConverter.class);
    public static final String format = "yyyy-MM-dd";
    public static final String displayFormat = "yyyy-MM-dd HH:mm";

    public Object convertFromString(Map ctx, String[] values, Class arg2) {
        if (values[0] == null || values[0].trim().equals("")) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(values[0]);
        } catch (ParseException pe) {
        	LOG.error("error converting value ["+values[0]+"] to Date ", pe);
        }
        return null;
    }

    public String convertToString(Map context, Object o) {

        if (o instanceof Date) {
            SimpleDateFormat sdf = new SimpleDateFormat(displayFormat);
            return sdf.format((Date) o);
        }
        return "";
    }



} 